package pri.lt.parser;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

import java.util.Date;

public class ParserManager {

    public static Parser getParser(String url) {
        if (canParse(QQNewParser.getInstance(), url)) {
            return QQNewParser.getInstance();
        } else if (canParse(SinaParser.getInstance(), url)) {
            return SinaParser.getInstance();
        } else if (canParse(SogouWeixinParser.getInstance(), url)) {
            return SogouWeixinParser.getInstance();
        }
        return null;
    }

    public static Parser.ParserResult parser(String url) {
        Parser parser = getParser(url);
        if (parser == null) {
            return  null;
        } else {
            return parser.parse(url);
        }
    }

    public static Parser.HotCommentResult getHotCommentResult(String url, Parser.ParserResult parserResult) {
        Date now = new Date();
        long elapseTime = now.getTime() - parserResult.publishTime.getTime();
        if (elapseTime > 1000 * 60 * 60 *36) {
            return null;
        }
        Parser parser = getParser(url);
        if (parser == null) {
            return  null;
        } else {
            return parser.getHotCommentResult(url, parserResult);
        }
    }

    public static boolean isDomainSupport(String url) {
        boolean isDomainSupport = false;
        if (QQNewParser.getInstance().isDomainSupport(url)) {
            isDomainSupport = true;
        }
        if (SinaParser.getInstance().isDomainSupport(url)) {
            isDomainSupport = true;

        }if (SogouWeixinParser.getInstance().isDomainSupport(url)) {
            isDomainSupport = true;
        }


        return  isDomainSupport;
    }


    public static boolean isUrlCanParse(String url) {
        boolean isUrlCanParse = false;
        if (QQNewParser.getInstance().isUrlCanParse(url)) {
            isUrlCanParse = true;
        }
        if (SinaParser.getInstance().isUrlCanParse(url)) {
            isUrlCanParse = true;
        }
        if (SogouWeixinParser.getInstance().isUrlCanParse(url)) {
            isUrlCanParse = true;
        }

        return  isUrlCanParse;
    }

    public static boolean isDomainDirectory(String url) {
        boolean isDomainDirectory = false;
        if (QQNewParser.getInstance().isDomainDirectory(url)) {
            isDomainDirectory = true;
        }
        if (SinaParser.getInstance().isDomainDirectory(url)) {
            isDomainDirectory = true;
        }
        if (SogouWeixinParser.getInstance().isDomainDirectory(url)) {
            isDomainDirectory = true;
        }

        return  isDomainDirectory;
    }

    private static boolean canParse(Parser parser, String url) {
        if (parser.isDomainSupport(url) || parser.isDomainDirectory(url) || parser.isUrlCanParse(url)) {
            return true;
        }
        return false;
    }
}
